package com.leyou.user.controller;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.utils.CookieUtils;
import com.leyou.user.config.JwtProperties;
import com.leyou.user.pojo.Address;
import com.leyou.user.pojo.Areas;
import com.leyou.user.pojo.City;
import com.leyou.user.pojo.Province;
import com.leyou.user.service.AddressService;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@EnableConfigurationProperties(JwtProperties.class)
public class AddressController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private AddressService addressService;

    @GetMapping("province")
    public ResponseEntity<List<Province>> queryAllProvince() {
        try {
            List<Province> provinces = this.addressService.queryAllProvince();
            if (CollectionUtils.isEmpty(provinces)) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(provinces);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("city")
    public ResponseEntity<List<City>> queryCityByProvinceId(@RequestParam("pid")String pid) {
        try {
            List<City> cities = this.addressService.queryCityByProvinceId(pid);
            if (CollectionUtils.isEmpty(cities)) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(cities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("areas")
    public ResponseEntity<List<Areas>> queryAreasByCityId(@RequestParam("cid")String cid) {
        try {
            List<Areas> areas = this.addressService.queryAreasByCityId(cid);
            if (CollectionUtils.isEmpty(areas)) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(areas);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



    @GetMapping("address")
    public ResponseEntity<List<Address>> queryAddress(HttpServletRequest request) {
        try {
            UserInfo userInfo = this.getUserInfo(request);
            if (userInfo == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            List<Address> addresses = this.userService.queryAddressByUserId(userInfo.getId());
            if (CollectionUtils.isEmpty(addresses)) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(addresses);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 新增地址
     * @param address
     * @param request
     * @return
     */
    @PostMapping("addAddress")
    public ResponseEntity<Void> addAddress(@RequestBody Address address, HttpServletRequest request) {
        try {
            System.out.println(address);
            UserInfo userInfo = this.getUserInfo(request);
            if (userInfo == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            address.setUserId(userInfo.getId());
            this.userService.addAddress(address);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @DeleteMapping("deleteAdd")
    public ResponseEntity<Void> deleteAdd(@RequestParam("id") Long id) {
        try {
            this.userService.deleteAdd(id);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    public  UserInfo getUserInfo(HttpServletRequest request) {

        String token = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            if (userInfo == null) {
                return null;
            }
            return userInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PutMapping("setDefaultAddress")
    public ResponseEntity<Void> setDefaultAddress(@RequestParam("id")Long id,HttpServletRequest request) {

        try {
            UserInfo userInfo = this.getUserInfo(request);
            if (userInfo == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            this.userService.setDefaultAddressbyUserId(id,userInfo.getId());
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("queryAddersById")
    public ResponseEntity<Address> queryAddersById(@RequestParam("id")Long id) {

        try {
            Address address = this.userService.queryAddersById(id);
            if (address == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
            }
            return ResponseEntity.ok(address);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PutMapping("updateAddress")
    public ResponseEntity<Void> upadteAddress(@RequestBody Address address,HttpServletRequest request) {
        try {
            System.out.println(address);
            UserInfo userInfo = this.getUserInfo(request);
            if (userInfo == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            address.setUserId(userInfo.getId());
            this.userService.upadtaAddress(address);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
